﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Data;
using DotSpatial.Controls;
using GeostatisticalTool.Classes;

namespace GeostatisticalTool.Forms
{
    public partial class DefineAreaStudy : System.Windows.Forms.Form
    {
        private IMap map;
        private Extent ext;
        private string name;

        private Clip clip;

        public Clip Clip
        {
            get { return clip; }
            set { clip = value; }
        }

        public Extent Ext
        {
            get { return ext; }
            set { this.ext = value;
            CopyExtentData();
            }
        }

        private void MapFrame_LayerAdded(object sender, EventArgs e)
        { 
          this.uxLayer.Items.Clear();
          int idv=0;
          int i = 0;




          foreach (IMapLayer ila in map.GetFeatureLayers())
            {
                    this.uxLayer.Items.Add(ila.LegendText);

                    if (ila.LegendText == this.name)
                        idv = i;

                    i++;
                
            }

            foreach (IMapGroup ila1 in map.MapFrame.GetAllGroups())
            {
                foreach (IMapLayer ila in ila1)
                {
                      this.uxLayer.Items.Add(ila.LegendText);

                      if (ila.LegendText == this.name)
                          idv = i;

                        i++;
                    }
                }
            

            if (this.uxLayer.Items.Count > 0)
            {
                this.uxLayer.SelectedIndex = idv;
            }
        }



        public void SetMap(IMap map, string mapName)
        {
            
         this.name= mapName;
         this.map=map;
         int i = 0;
         int idv = 0;
            if (map!=null)
            {
                foreach (IMapLayer ila in map.GetPolygonLayers())
                {
                    this.uxLayerMask.Items.Add(ila.LegendText);
                }


   
            foreach (IMapLayer ila in map.GetFeatureLayers())
            {
               
                    this.uxLayer.Items.Add(ila.LegendText);

                    if (ila.LegendText == mapName)
                        idv = i;

                    i++;
                
            }

            foreach (IMapGroup ila1 in map.MapFrame.GetAllGroups())
            {
                foreach (IMapLayer ila in ila1)
                {
                      this.uxLayer.Items.Add(ila.LegendText);

                      if (ila.LegendText == mapName)
                          idv = i;

                        i++;
                    }
                }
            }

            if (this.uxLayer.Items.Count > 0)
            {
                this.uxLayer.SelectedIndex = idv;
            }
        }

        public DefineAreaStudy()
        {
            InitializeComponent();

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void uxLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (IMapLayer ila in map.GetFeatureLayers())
            {
                    if (uxLayer.Text==ila.LegendText)
                        this.ext=ila.Extent;
               
            }

            foreach (IMapGroup ila1 in map.MapFrame.GetAllGroups())
            {
                foreach (IMapLayer ila in ila1)
                {
                    if (uxLayer.Text==ila.LegendText)
                        this.ext=ila.Extent;
                   
                }
            }

            CopyExtentData();
        }

        private void CopyExtentData()
        {
            uxTop.Text = this.ext.MaxY.ToString();
            uxBotton.Text= this.ext.MinY.ToString();
            uxLeft.Text =this.ext.MinX.ToString();
            uxRight.Text=this.ext.MaxX.ToString();
       
        }

        private void DefineAreaStudy_Load(object sender, EventArgs e)
        {
           map.MapFrame.LayerAdded +=new EventHandler<DotSpatial.Symbology.LayerEventArgs>(MapFrame_LayerAdded);
        }

        private void uxAccept_Click(object sender, EventArgs e)
        {
            if (this.uxLayer.Items.Count > 0)
            {
                if (Validator.IsDouble(uxTop) &&
                    Validator.IsDouble(uxBotton) &&
                    Validator.IsDouble(uxLeft) &&
                    Validator.IsDouble(uxRight))
                {
                    this.ext = new Extent();
                    this.ext.MinY = Convert.ToDouble(uxBotton.Text);
                    this.ext.MaxY = Convert.ToDouble(uxTop.Text);
                    this.ext.MinX = Convert.ToDouble(uxLeft.Text);
                    this.ext.MaxX = Convert.ToDouble(uxRight.Text);
                }
            }
        }

        private void uxMask_CheckedChanged(object sender, EventArgs e)
        {
            uxLayerMask.Enabled = uxMask.Checked;
            if (!uxLayerMask.Enabled)
            {
                clip = null;
            }
        }

        private void uxLayerMask_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (IMapLayer ila in map.GetPolygonLayers())
            {
                   if (ila.LegendText== uxLayerMask.Text)
                   {
                       IFeatureSet fea = (IFeatureSet)ila.DataSet;
                       clip = new Clip(fea);
                   }

            }
        }
    }
}
